{extend name="common/__base"}

{block name="body"}
<fieldset class="table-search-fieldset">
    <legend>搜索信息</legend>
    <div>
        <form class="layui-form layui-form-pane">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="username" id="username" placeholder="用户名">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">昵称</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="nickname" id="nickname" placeholder="昵称">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="search"><i class="layui-icon layui-icon-search"></i>搜索</button>
                </div>
            </div>
        </form>
    </div>
</fieldset>

<table id="table" lay-filter="table"></table>

<!--状态-->
<script id="status" type="text/html">
    <input type="checkbox" lay-skin="switch" lay-text="正常|禁用" lay-filter="status" value="{{d.id}}" {{d.status == 1 ? "checked" : ""}}>
</script>

<!--操作-->
<script id="operation" type="text/html">
    <button class="layui-btn layui-btn-xs" lay-event="edit">编辑</button>
    <button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</button>
</script>

<!--工具栏-->
<script id="toolbar" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del">删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="refresh">刷新</button>
    </div>
</script>
{/block}

{block name="js"}
<script>
    layui.use(['table', 'form', 'miniTab'], function () {
        let table = layui.table,
            form = layui.form,
            $ = layui.jquery,
            miniTab = layui.miniTab;
        // 数据表格
        let dataTable = table.render({
            elem: "#table",
            id: "table",
            cols: [[
                {type: "checkbox"},
                {title: "角色", field: "group_name"},
                {title: "用户名", field: "username"},
                {title: "昵称", field: "nickname"},
                {title: "状态", toolbar: "#status"},
                {title: "操作", toolbar: "#operation"}
            ]],
            url: "{:url('admin/admin/adminList')}",
            method: "post",
            parseData: function (res) {
                return {
                    code: res.code,
                    msg: res.msg,
                    data: res.data.list,
                    count: res.data.count
                };
            },
            page: true,
            toolbar: "#toolbar",
            defaultToolbar: []
        });

        // 搜索
        form.on("submit(search)", function () {
            let username = $("#username").val();
            let nickname = $("#nickname").val();
            dataTable.reload({
                where: {
                    username: username,
                    nickname: nickname
                },
                page: {
                    curr: 1
                }
            });
            return false;
        });

        // 工具栏
        table.on("toolbar(table)", function (obj) {
            if (obj.event == "add") {
                miniTab.openNewTabByIframe({
                    title: "添加管理员",
                    href: "{:url('admin/admin/addAdmin')}"
                });
            } else if (obj.event == "del") {
                let delData = table.checkStatus("table");
                if (delData.data.length == 0) {
                    layer.msg("至少选择一个");
                } else {
                    layer.confirm("确定删除吗？", {icon: 3, title: "删除管理员"}, function () {
                        let admin_ids = delData.data.map(x => {return x.id}).join(",");
                        $.post("{:url('admin/admin/delAdmins')}", {admin_ids: admin_ids}, function (res) {
                            if (res.code == 1) {
                                layer.msg(res.msg, function () {
                                    location.reload();
                                });
                            } else {
                                layer.msg(res.msg);
                            }
                        }, "json");
                    });
                }
            } else if (obj.event == "refresh") {
                location.reload();
            }
        });

        // 状态
        form.on("switch(status)", function (obj) {
            let status = 1;
            if (obj.elem.checked) {
                status = 1;
            } else {
                status = 0;
            }
            $.post("{:url('admin/admin/setAdmin')}", {status: status, id: obj.value}, function (res) {
                if (res.code == 1) {
                    layer.msg(res.msg);
                } else {
                    obj.elem.checked = !status;
                    form.render();
                    layer.msg(res.msg);
                }
            }, "json");
        });

        // 工具
        table.on("tool(table)", function (obj) {
            if (obj.event == "edit") {
                miniTab.openNewTabByIframe({
                    title: "编辑管理员",
                    href: "{:url('admin/admin/editAdmin')}?id=" + obj.data.id
                });
            } else if (obj.event == "del") {
                layer.confirm("确定删除吗？", {icon: 3, title: "删除管理员"}, function () {
                    $.post("{:url('admin/admin/delAdmins')}", {admin_ids: obj.data.id}, function (res) {
                        layer.msg(res.msg);
                        if (res.code == 1) {
                            obj.del();
                        }
                    }, "json");
                });
            }
        });
    });
</script>
{/block}